package com.kc.business.service.impl;

import cn.hutool.http.HttpStatus;
import com.kc.business.domain.*;
import com.kc.business.domain.dto.BizWalletRecordDTO;
import com.kc.business.domain.order.entity.BizOrderGoods;
import com.kc.business.enums.BusinessTypeEnum;
import com.kc.business.enums.OrderWithdrawStatusEnum;
import com.kc.business.mapper.*;
import com.kc.business.service.IBizPlatformWalletService;
import com.kc.common.utils.WxPartnerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName BizPlatformWalletServiceImpl
 * @Description TODO
 * @Author wangdongliang
 * @Date 2021/3/26 12:08
 */
@Service
@Slf4j
public class BizPlatformWalletServiceImpl implements IBizPlatformWalletService {
    @Resource
    private BizCelebrityMapper bizCelebrityMapper;
    @Resource
    private BizScenicBusinessMapper bizScenicBusinessMapper;
    @Resource
    private BizTourguideMapper bizTourguideMapper;
    @Resource
    private BizScenicMapper bizScenicMapper;
    @Resource
    private BizGoodsOrderMapper bizGoodsOrderMapper;
    @Resource
    private BizUserWithdrawRecordsMapper bizUserWithdrawRecordsMapper;
    @Resource
    private WxPartnerService wxPartnerService;
    @Resource
    private BizGoodsOrderShareProfitRecordMapper bizGoodsOrderShareProfitRecordMapper;
    @Resource
    private BizOrderGoodsMapper bizOrderGoodsMapper;

    @Override
    public String getSubMchid(Integer userId, Integer type) {
        //类型:0-导游，1景区，2名家，3市场，4商家
        String subMchid = "";
        if(type.equals(BusinessTypeEnum.CELEBRITY.getValue())) {
            BizCelebrity bizCelebrity = new BizCelebrity();
            bizCelebrity.setUserId(userId);
            subMchid = bizCelebrityMapper.selectOne(bizCelebrity).getSubMchid();
        }else if(type.equals(BusinessTypeEnum.BUSINESS.getValue())) {
            BizScenicBusiness bizScenicBusiness = new BizScenicBusiness();
            bizScenicBusiness.setId(userId);
            subMchid = bizScenicBusinessMapper.selectOne(bizScenicBusiness).getSubMchid();
        }else if(type.equals(0)){
            BizTourguide bizTourguide = new BizTourguide();
            bizTourguide.setUserId(userId);
            subMchid = bizTourguideMapper.selectOne(bizTourguide).getSubMchid();
        }else if(type.equals(BusinessTypeEnum.SCENIC.getValue())){
            BizScenic bizScenic = new BizScenic();
            bizScenic.setId(userId);
            BizScenic scenic = bizScenicMapper.selectByPrimaryKey(userId);
            subMchid = scenic.getSubMchid();
        }
//        else if(type.equals(GoodsBusinessTypeEnum.MARKET.getValue())){
//            subMchid = kcMarketBusinessMapper.selectOne(new LambdaQueryWrapper<KcMarketBusiness>().eq(KcMarketBusiness::getUserId,userId)).getSubMchid();
//        }
//        if(StringUtils.isEmpty(subMchid)){
//            throw new CustomizeException("二级商户号错误");
//        }
        return subMchid;
    }

    @Override
    public BigDecimal getFinishOrderMoney(Integer merchId, Integer type) {
        Double  payMoney = bizGoodsOrderMapper.getFinishOrderMoney(merchId,type,3);
        if(payMoney == null){
            return new BigDecimal("0.0");
        }
        return new BigDecimal(payMoney.toString());
    }

    @Override
    public BigDecimal getWithdrawing(String subMchid) {
        Double withdrawAmount = bizUserWithdrawRecordsMapper.getWithdrawByStatus(subMchid, OrderWithdrawStatusEnum.ACCEPTING.getValue());
        if(withdrawAmount == null){
            return new BigDecimal("0.0");
        }
        return new BigDecimal(withdrawAmount.toString());
    }

    @Override
    public BigDecimal getWithdraw(String subMchid) {
        Double withdrawAmount  = bizUserWithdrawRecordsMapper.getWithdrawByStatus(subMchid, OrderWithdrawStatusEnum.SUCCESS.getValue());
        if(withdrawAmount == null){
            return new BigDecimal("0.0");
        }
        return new BigDecimal(withdrawAmount.toString());
    }

    @Override
    public BigDecimal getCanWithdraw(String subMchid) {
        Map<String, Object> map = wxPartnerService.queryOnlineBalance(subMchid);
        if (HttpStatus.HTTP_OK == Integer.parseInt(String.valueOf(map.get("httpStatusCode")))) {
            return new BigDecimal(String.valueOf(map.get("available_amount")))
                    .divide(new BigDecimal("100"), 2, BigDecimal.ROUND_HALF_UP);
        }
        return new BigDecimal("0.0");
    }

    @Override
    public List<BizWalletRecordDTO> getWalletRecord(String subMchid) {
        //查询收入列表
        BizGoodsOrderShareProfitRecord bizGoodsOrderShareProfitRecord = new BizGoodsOrderShareProfitRecord();
        bizGoodsOrderShareProfitRecord.setSubMchid(subMchid);
        List<BizGoodsOrderShareProfitRecord> bizGoodsOrderShareProfitRecordList = bizGoodsOrderShareProfitRecordMapper.select(bizGoodsOrderShareProfitRecord);
        //遍历拼接需要返回的数据
        List<BizWalletRecordDTO> bizWalletRecordDTOList = new ArrayList<>();
        BizOrderGoods bizOrderGoods = new BizOrderGoods();
        for(BizGoodsOrderShareProfitRecord goodsOrderShareProfitRecord : bizGoodsOrderShareProfitRecordList){
            BizWalletRecordDTO bizWalletRecordDTO = new BizWalletRecordDTO();
            bizWalletRecordDTO.setOrderSn(goodsOrderShareProfitRecord.getOrderSn());
            bizWalletRecordDTO.setOrderIncome(new BigDecimal(goodsOrderShareProfitRecord.getAmount()).divide(new BigDecimal("100")));
            bizWalletRecordDTO.setOrderTime(bizGoodsOrderShareProfitRecord.getOrderTime());
            bizWalletRecordDTO.setOrderType("销售收入");
            //查询订单下商品数量
            bizOrderGoods.setOrderId(bizGoodsOrderShareProfitRecord.getOrderId() == null ? 0 : bizGoodsOrderShareProfitRecord.getOrderId());
            Integer goodsNum = bizOrderGoodsMapper.selectCount(bizOrderGoods);
            bizWalletRecordDTO.setOrderDesc("共计"+goodsNum+"件商品");
            bizWalletRecordDTOList.add(bizWalletRecordDTO);
        }
        return bizWalletRecordDTOList;
    }
}
